<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-touch-fullscreen" content="yes">
<title>html validator</title>
</head>
<body>
<label>
    检查是否标签闭合完整
    <textarea id="inp" style="width: 500px;height: 500px;"></textarea>
</label>
<br/>
<button id="start">开始</button>
<script src="/kissy/build/seed-debug.js"></script>
<script src="/kissy/src/package.js"></script>
<script>
    KISSY.use("html-parser,node", function (S, HtmlParser,$) {
        var Lexer = HtmlParser.Lexer;
        $("#start").on("click", function () {
            var inp = $("#inp").val();
            var lexer = new Lexer(inp);
            var stack = [];
            var node = lexer.nextNode();
            while (node) {
                if (node.nodeType == 1 && !node.isSelfClosed) {
                    if (node.isEndTag()) {
                        var current = stack[stack.length - 1];
                        if (!current || current.tagName != node.tagName) {
                            alert("结束标签没有开始标签 : \n" + node);
                            if (inp.length > 250) {
                                alert("周围 html ： \n\n" + inp
                                        .substring(node.startPosition - 100,
                                        node.endPosition + 100));
                            }
                            return;
                        } else {
                            stack.length = stack.length - 1;
                        }
                    } else {
                        stack.push(node);
                    }
                }

                if (node.nodeName == 'textarea') {
                    lexer.parseCDATA(false, 'textarea');
                }
                if (node.nodeName == "style" || node.nodeName == "script") {
                    lexer.parseCDATA(true, node.nodeName);
                }
                node = lexer.nextNode();
            }

            if (stack.length) {
                alert("开始标签没有结束标签：\n" + stack.join("\n"));
            } else {
                alert("ok");
            }
        });
    });
</script>
</body>
</html>